package com.ygs.secs.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ygs.secs.model.Clzaa;
import com.ygs.secs.model.Profession;
import com.ygs.secs.service.ClzaaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 班级管理模块控制器
 */

@Controller
@RequestMapping("/admin")
public class ClzaaManagerController {
    @Autowired
    ClzaaService clzaaService;

    /**
     * 查询班级信息
     *
     * @param professionId 专业ID
     * @return 班级集合
     */
    @RequestMapping("/getClzaaByProfessionId")
    @ResponseBody
    public Map<String, Object> getClzaaByProfessionId(
            @RequestParam(value = "professionId", defaultValue = "1") Integer professionId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int limit) {
        Map<String, Object> classesData = new HashMap<>();
        try {
            //分页查询
            Page startPage = PageHelper.startPage(page, limit);
            List<Clzaa> clzaaList = clzaaService.getClzaaByProfessionId(professionId);
            if (clzaaList.size() > 0) {
                classesData.put("code", 200);
                classesData.put("msg", "数据查询成功");
                classesData.put("data", clzaaList);
                classesData.put("count", startPage.getTotal());
                return classesData;
            } else {
                classesData.put("code", 201);
                classesData.put("msg", "数据为null，请先添加");
                return classesData;
            }
        } catch (Exception e) {
            classesData.put("code", 500);
            classesData.put("msg", "system error");
            return classesData;
        }
    }

    /**
     * 删除班级
     *
     * @param clazzid 班级ID
     * @return 操作结果
     */
    @RequestMapping("/delClzaa")
    @ResponseBody
    public Map<String, Object> delClzaa(@RequestParam("clazzid") Integer clazzid) {
        Map<String, Object> result = new HashMap<>();
        try {
            int res = clzaaService.deleteByPrimaryKey(clazzid);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "删除班级失败");
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "操作失败，请先删除所属信息");
            return result;
        }
    }

    /**
     * 修改班级信息
     *
     * @param jsonClzaa 班级实体json串
     * @return 操作结果
     */
    @RequestMapping("/updateClzaa")
    @ResponseBody
    public Map<String, Object> updateClzaa(String jsonClzaa) {
        Clzaa clzaa = JSONObject.parseObject(jsonClzaa, Clzaa.class);
        Map<String, Object> result = new HashMap<>();
        int res = clzaaService.updateByPrimaryKeySelective(clzaa);
        if (res > 0) {
            result.put("code", 200);
            result.put("msg", "操作成功");
        } else {
            result.put("code", 500);
            result.put("msg", "操作失败");
        }
        return result;
    }

    /**
     * 添加班级信息
     *
     * @param jsonClzaa 班级实体json串
     * @return 操作结果
     */
    @RequestMapping("/addClzaa")
    @ResponseBody
    public Map<String, Object> addClzaa(String jsonClzaa) {
        Map<String, Object> result = new HashMap<>();
        try {
            Clzaa clzaa = JSONObject.parseObject(jsonClzaa, Clzaa.class);
            int res = clzaaService.insertSelective(clzaa);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        } catch (Exception e) {
            result.put("code", 501);
            result.put("msg", "system error");
            return result;
        }
    }

    /**
     * 批量修改班级信息
     *
     * @param jsonArrayClzaa 班级实体数组json串
     * @return 操作结果
     */
    @RequestMapping(value = "/batchUpdateClzaa", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchUpdateClasses(String jsonArrayClzaa) {
        Map<String, Object> result = new HashMap<>();
        List<Clzaa> clzaaList = JSONArray.parseArray(jsonArrayClzaa, Clzaa.class);
        int res = clzaaService.updateBatchSelective(clzaaList);
        if (res > 0) {
            result.put("code", 200);
            result.put("msg", "操作成功");
        } else {
            result.put("code", 500);
            result.put("msg", "操作失败");
        }
        return result;
    }

    /**
     * 批量添加班级信息
     *
     * @param jsonArrayClzaa 班级数组json串
     * @return 操作结果
     */
    @RequestMapping(value = "/batchInsertClzaa", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchInsertClzaa(String jsonArrayClzaa) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Clzaa> classesList = JSONArray.parseArray(jsonArrayClzaa, Clzaa.class);
            int res = clzaaService.batchInsert(classesList);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 501);
            result.put("msg", "system error");
            return result;
        }
    }

    /**
     * 批量删除班级信息
     *
     * @param clzaas 班级id数组
     * @return 操作结果
     */
    @RequestMapping(value = "/batchDelClzaa", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchDelClasses(Integer[] clzaas) {
        Map<String, Object> result = new HashMap<>();
        try {
            int res = clzaaService.batchDelete(clzaas);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "操作失败，请先删除所属信息");
            return result;
        }
    }
}
